<?php

namespace app\cash\model;

use app\common\model\CommonModel;


class CashSettleInfoModel extends CommonModel{

    protected  $name = 'beautiful_cash_settle_info';

    protected  $pk   = 'settle_info_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function getById($where,$field='*')
    {
        $list = self::where($where)->field($field)->find();
        return $list;
    }

    public static function list($where,$order,$page=1,$limit=10,$field='*'){
        $query = self::alias('si')
            ->leftjoin('beautiful_cash_third_settle s','s.cash_third_settle_id=si.cash_third_settle_id')
            ->where(['si.is_delete'=>0])
            ->when(isset($where['order_id']),function ($query) use ($where){
                $query->where(['si.order_id'=>$where['order_id']]);
            })
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['si.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['si.status'=>$where['status']]);
            })->field($field);

        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        foreach ($list as $k=>$v){
            if($v['type']==7){
                $list[$k]['name'] = $v['settlement_name'];
            }
            $is_bf_refund = 0;
            if($v['refund_money']>0){
                $is_bf_refund = $v['refund_money']>=$v['money']?1:2;
            }
            $list[$k]['is_bf_refund'] = $is_bf_refund;
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time'] = date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
}

